<?php

namespace app\admin\controller;

use app\common\model\Member as MemberModel;
use app\common\model\Mzongpai as MzongpaiModel;
use app\common\model\MemberQuan as MemberQuanModel;
use app\common\model\Ilike as IlikeModel;
use app\common\model\Friends as FriendsModel;
use app\common\model\Gatherreg as GatherregModel;
use app\common\model\Recharge as RechargeModel;
use app\common\model\Manager as ManagerModel;
use app\common\model\ManagerDuty as ManagerDutyModel;
use app\common\model\Company as CompanyModel;
use app\common\model\Renzhen as RenzhenModel;
use app\common\model\ManagerCompany as ManagerCompanyModel;
use app\common\model\CompanyGongdengOrder as CompanyGongdengOrderModel;
use app\common\model\NewsComment as NewsCommentModel;
use app\common\model\Mjifen as MjifenModel;
use app\common\model\BuddhistService as BuddhistServiceModel;

use think\Db;
/**
 * 用户控制器
 * Class Member
 * @package app\index\controller
 */
class Member extends Base
{
    /**
     * [index  C端用户列表]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getIndex()
    {
        $model = new MemberModel();
        $rmodel = new RenzhenModel();

        $user = $this->user;
        $data = input('param.');
        $map = [];
        $this->getPageAndSize($data);
        if(!empty($data['manager'])){
            $lists = $this->getManager($data);
            writelog('查看法师列表',1,$user);
            return admin_json(1, '获取成功',$lists);
        }
        if(!empty($data['renzhen'])){
            $lists = $this->getRenzhen($data);
            writelog('查看法师认证',1,$user);
            return admin_json(1, '获取成功',$lists);
        }
        if(!empty($data['duty'])){
            $lists = $this->duty($data['comid']);
            writelog('查看职务列表--comid'.$data['comid'],1,$user);
            return admin_json(1, '获取成功',$lists);
        }
        if (is_array($data)) {
            if(!empty($data['con'])){
                $map['username|tel'] = ['like', "%" . $data['con'] . "%"];
            }
            if(!empty($data['sex'])) {
                $map['sex'] = $data['sex'];
            }
            if(!empty($data['start']) && !empty($data['end'])){
                $map['addtime'] = ['between',[strtotime($data['start']),strtotime($data['end'])]];
            }
        }

        try {
            $count = $model->getMemberCountByCondition($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getMemberByConditon($map, $this->from, $this->size);
            $zmodel = new MzongpaiModel();
            $qmodel = new MemberQuanModel();
            $imodel = new IlikeModel();
            $fmodel = new FriendsModel();
//            $cmodel = new CompanyGongdengOrderModel;
            $remodel = new RechargeModel;
            $ncmodel = new NewsCommentModel;
            $gmodel = new GatherregModel();
            $bmodel = new BuddhistServiceModel();
            $cmodel = new CompanyModel();
            foreach($lists as $k=>$v){
                if(empty($v['register_comid'])){
                    if(empty($v['tel'])){
                        $lists[$k]['comname'] = '未知';
                        if($v['eqno'] = 'app_apple_account'){
                            $lists[$k]['comname'] = 'APP';
                        }
                    }else{
                        $lists[$k]['comname'] = 'APP';
                    }
                }else{
                    $company = $cmodel->findData($v['register_comid'],'id','comname');
                    $lists[$k]['comname'] = $company['comname'];
                }
//                $cnum = $cmodel->getSum(['mid'=>$v['id'],'status'=>1],'total_price');
                $v['buddhist_num'] = $bmodel->getAllCount(['memid'=>$v['id']]);
                $renum = $remodel->getSum(['memid'=>$v['id'],'states'=>1],'amt');
                $lists[$k]['shankuan'] = $renum;
                $lists[$k]['invite_num'] = $model->getAllCount(['agent_id'=>$v['id']]);
                $lists[$k]['baoming_num'] = $gmodel->getAllCount(['memid'=>$v['id']]);
                $lists[$k]['pinlun'] = $ncmodel->getCount(['c.mid'=>$v['id'],'c.parid'=>0]);
                $renzheng = $rmodel->findData($v['id'],'uid','status');
                if(empty($renzheng)){
                    $lists[$k]['renzhengstatus'] = 3;
                    if($renzheng['status'] === 0){
                        $lists[$k]['renzhengstatus'] = 0;
                    }
                }else{
                    $lists[$k]['renzhengstatus'] = $renzheng['status'];
                }

                if(!empty($v['qrcode'])){
                    $lists[$k]['qrcode'] = 'http://'.$_SERVER['HTTP_HOST'].'/'.$v['qrcode'];
                }
                $http = substr($v['img'],0,4);
                if($http != 'http'){
                    if(empty($v['img'])){
                        $lists[$k]['img'] = 'https://fushan.oss-cn-hangzhou.aliyuncs.com/nouser.png';
                    }else{
                        $lists[$k]['img'] = config('domain_name').$v['img'];
                    }
                }
                $lists[$k]['url'] = 'https://wx.fushan8.com/#/invitePage?id='.$v['id'];
                if(!empty($v['zid'])){
                    $zongpzi = $zmodel->findData($v['zid']);
                    $lists[$k]['zongpai'] = $zongpzi['name'];
                }else{
                    $lists[$k]['zongpai'] = '';
                }
                $lists[$k]['addtime'] = date('Y:m:d H:i:s',$lists[$k]['addtime']);
                $map1['member_id'] = $v['id'];
                $quan = $qmodel->getNum($map1);
                $lists[$k]['quan'] = $quan;
                $lists[$k]['guanzhu'] = $imodel->getAllCount(['memid'=>$v['id'],'status'=>1]);
                $map2['memid|friend_id'] = $v['id'];
                $map2['states'] = 1;
                $lists[$k]['friends'] = $fmodel->getAllCount($map2);
                if($v['thawingtime'] === 0 || $v['thawingtime'] > time()){
                    $lists[$k]['status'] = 0;
                }else{
                    $lists[$k]['status'] = 1;
                }
                $lists[$k]['thawingtime'] = date('Y-m-d H:i:s',$v['thawingtime']);
            }
        } catch (\Exception $e) {
            return admin_json(-1,$e->getMessage());
        }

        writelog('查看C端用户列表',1,$user);

        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [duty  寺院id]
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function duty($comid)
    {
        $model = new ManagerDutyModel;
        $map = [];
        try{
            $map['comid'] = $comid;
            $map['status'] = 1;
            $list = $model->getAllData($map);
            $moren = $model->limit(['comid'=>0],0,9);
            array_unshift($list,$moren);
        }catch (\Exception $e) {
            return admin_json(-1,$e->getMessage());
        }

        return $list;
    }
    /**
     * [getRenzhen  获取认证列表]
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getRenzhen($data)
    {
        $map = [];
        $this->getPageAndSize($data);
        $model = new RenzhenModel();

        try{
            $field = 'r.id,m.username,r.idcard,r.status,r.fahao,r.siyuan,r.card_pic,r.addtime,r.info';
            $count = $model->getCounts($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlists($map, $this->from, $this->size,$field);
            foreach($lists as $k=>$v){
                $card = explode(',',$v['card_pic']);
                foreach($card as $k1=>$v1){
                    $http = substr($v1,0,4);
                    if($http != 'http'){
                        $card[$k1] = config('domain_name').'/'.$v1;
                    }
                }
                $lists[$k]['card_pic'] = $card;
            }
        }catch (\Exception $e) {
            return admin_json(-1,$e->getMessage());
        }
        return array([
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [postEdit  获取法师]
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getManager($data)
    {
        $map = [];
        $user = $this->user;
        $this->getPageAndSize($data);
        if (is_array($data)) {
            if(!empty($data['con'])){
                $map['tel|username'] = ['like', "%" . $data['con'] . "%"];
            }
            if(!empty($data['start']) && !empty($data['end'])){
                $map['addtime'] = ['between',[strtotime($data['start']),strtotime($data['end'])]];
            }
        }
        $model = new ManagerModel();
        $mmodel = new MemberModel();
        $zmodel = new MzongpaiModel();
        $cmodel = new CompanyModel();
        $qmodel = new MemberQuanModel();
        $imodel = new IlikeModel();
        $fmodel = new FriendsModel();
        $mcmodel = new ManagerCompanyModel();
        try{
            if($user['is_zhu'] == 2){
                $map['admin_id'] = $user['id'];
            }
            $map['states'] = ['neq',3];
            $count = $model->getCountByCondition($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getByConditon($map, $this->from, $this->size);
            if($user['is_zhu'] == 2 && $this->from == 0){
                $fushan = $model->findData(43,'id');
                $lists[] = $fushan;
            }
            foreach($lists as $k=>$v){
                $mem = $mmodel->findData($v['tel'],'tel');
                $mclist = $mcmodel->getAllData(['mid'=>$v['id'],'status'=>1],'id','id,comid,dutyid');
                if(empty($mclist) && !empty($v['comid'])){
                    $mcmodel->insertdata(['comid'=>$v['comid'],'dutyid'=>0,'mid'=>$v['id'],'addtime'=>time()]);
                }else{
                    foreach($mclist as $k1=>$v1){
                        $company = $cmodel->findData($v1['comid'],'id','comname');
                        $mclist[$k1]['comname'] = $company['comname'];
                    }
                }
                $lists[$k]['ma'] = $mclist;

                $lists[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                if(!empty($mem['qrcode'])){
                    $lists[$k]['qrcode'] = 'http://'.$_SERVER['HTTP_HOST'].'/'.$mem['qrcode'];
                }

                if(!empty($mem)){
                    $lists[$k]['sex'] = $mem['sex'];
                    $lists[$k]['city'] = $mem['city'];
                    $lists[$k]['bathday'] = $mem['bathday'];
                    if($mem['identity'] == 1){
                        $mmodel->editData(['id'=>$mem['id'],'identity'=>2]);
                    }
                    if(!empty($mem['zid'])){
                        $pai = $zmodel->findData($mem['zid']);
                        $lists[$k]['zongpai'] = $pai['name'];
                    }
                    $map1['member_id'] = $mem['id'];
                    $quan = $qmodel->getNum($map1);
                    $lists[$k]['quan'] = $quan;
                    $lists[$k]['jifen'] = $mem['jifen'];
                    $lists[$k]['guanzhu'] = $imodel->getAllCount(['memid'=>$mem['id']]);
                    $lists[$k]['friends'] = $fmodel->getAllCount(['memid'=>$mem['id']]);
                    $lists[$k]['zid'] = $mem['zid'];
                    $lists[$k]['url'] = 'https://wx.fushan8.com/#/invitePage?id='.$mem['id'];
                }

            }

        }catch (\Exception $e) {
            return admin_json(-1,$e->getMessage());
        }
        return array(
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists  //列表数据
        );
    }
    /**
     * [postEdit  修改用户数据]
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postEdit()
    {
        $data = input('param.');
        $user = $this->user;
        if(!empty($data['manager'])){
            $model = new ManagerModel();
            $name = '法师';
            $mmodel = new MemberModel();
            $memager = $model->findData($data['manager']['id'],'id','tel');
            if(!empty($data['manager']['tel'])){
                if($memager['tel'] != $data['manager']['tel']){
                    $mmodel->editByTel(['tel'=>$data['manager']['tel']],['tel'=>$memager['tel']]);
                }
            }
            $member = $mmodel->findData($memager['tel'],'tel','id');
            if(!empty($data['manager']['username'])){
                $mmodel->editData(['id'=>$member['id'],'username'=>$data['manager']['username']]);
            }
            if(!empty($data['manager']['zid'])){
                $mmodel->editData(['id'=>$member['id'],'zid'=>$data['manager']['zid']]);
            }
            if(!empty($data['manager']['city'])){
                $mmodel->editData(['id'=>$member['id'],'city'=>$data['manager']['city']]);
            }
            if(!empty($data['manager']['bathday'])){
                $mmodel->editData(['id'=>$member['id'],'bathday'=>$data['manager']['bathday']]);
            }
            if(!empty($data['manager']['sex'])){
                $mmodel->editData(['id'=>$member['id'],'sex'=>$data['manager']['sex']]);
            }
            if(!empty($data['manager']['info'])){
                if($data['manager']['info'] == 1){
                    $managernum = $model->getCountByCondition(['info'=>1]);
                    if($managernum >= 4){
                        return admin_json(-1,'推荐法师已超出推荐个数，已有'.$managernum.'个');
                    }
                }
            }
            $data = $data['manager'];
        }elseif(!empty($data['member'])){
            $model = new MemberModel();
            $mmodel = new ManagerModel();
            $name = '用户';
            if(!empty($data['identity'])){
                if($data['identity'] == 2){
                    $mem = $model->findData($data['id'],'id','tel');
                    $man = $mmodel->findData($mem['tel'],'tel');
                    if(empty($man)){
                        $mmodel->addData(['tel'=>$mem['tel'],'addtime'=>time()]);
                    }
                }
            }

            if(!empty($data['status'])){
                if($data['status'] == 1){
                    $time = 3600*24*7;
                    $data['thawingtime'] = time() + $time;
                }elseif($data['status'] == 2){
                    $time = 3600*24*30;
                    $data['thawingtime'] = time() + $time;
                }elseif($data['status'] == 3){
                    $time = 3600*24*30*6;
                    $data['thawingtime'] = time() + $time;
                }elseif($data['status'] == 4){
                    $data['thawingtime'] = 0;
                }elseif($data['status'] == 5){
                    $data['thawingtime'] = 1;
                }
            }
        }
        if(!empty($data['renzhen'])){
            $model = new RenzhenModel();
            $name = '法师认证';
            if($data['status'] == 1){
                $mmodel = new MemberModel();
                $mamodel = new ManagerModel();
                $cmodel = new CompanyModel();
                $renzheng = $model->findData($data['id'],'id');
                $card = explode(',',$renzheng['card_pic']);
                $member = $mmodel->findData($renzheng['uid'],'id','tel');
                $manager = $mamodel->findData($member['tel'],'tel','id,tel,states');
                if(empty($manager)){
                    $mid = $mamodel->addData(['tel'=>$member['tel'],'username'=>$renzheng['fahao'],'addtime'=>time(),'idcard'=>$renzheng['idcard'],'zcard'=>$card[0],'fcard'=>$card[1],'jiedie'=>$card[2]]);
                }else{
                    if($manager['states'] == 3){
                        $mamodel->editData(['id'=>$manager['id'],'states'=>1,'tel'=>$member['tel'],'username'=>$renzheng['fahao'],'addtime'=>time(),'idcard'=>$renzheng['idcard'],'zcard'=>$card[0],'fcard'=>$card[1],'jiedie'=>$card[2]]);
                    }
                    $mid = $manager['id'];
                }
                $member = $mmodel->findData($renzheng['uid'],'id','id,quality');
                $mmodel->editData(['id'=>$member['id'],'identity'=>2]);
                add_quality($member['id'],22,$member['quality'],3);
                $company = $cmodel->getAllData(['comname'=>$renzheng['siyuan']]);
            }else{
                $mid = 0;
            }
        }
        try{
            $id = $model->editData($data);
        }catch (\Exception $e) {
            return admin_json(-1,'修改失败'.$e->getMessage());
        }
        writelog('修改'.$name.$id.'的内容',1,$user);
        if(!empty($data['renzhen'])){
            if(empty($company)){
                $status = 0;
            }else{
                $status = 1;
            }
            return admin_json(1, '审核成功',['managerid'=>$mid,'status'=>$status]);
        }
        return admin_json(1, '修改成功');
    }
    /**
     * [postAdd  添加法师]
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postAdd()
    {
        $data = input('param.');
        $model = new ManagerModel();
        $mmodel = new MemberModel();
        $name = '法师';
        $user = $this->user;
        try{
            $result = $model->findData($data['manager']['tel'],'tel');
            $mem = $mmodel->findData($data['manager']['tel'],'tel','id');
            if(empty($mem) && empty($data['pswd'])){
                return admin_json(3, '还不是平台用户！');
            }

            if(!empty($result)){
                if($result['states'] != 3){
                    return admin_json(2, '法师已存在！');
                }else{
                    if($user['is_zhu'] == 2){
                        return admin_json(2, '法师已存在！');
                    }
                    $data['manager']['id'] = $result['id'];
                    $id = $model->editData($data['manager']);
                }
            }else{
                $data['manager']['admin_id'] = $user['id'];
                $data['manager']['addtime'] = time();
                $id = $model->addData($data['manager']);
            }
            if(empty($mem) && !empty($data['pswd'])){
                $mmodel->addData(['tel'=>$data['manager']['tel'],'pswd'=>md5($data['pswd']),'img'=>config('admin.nouser_img'),'identity'=>2,'addtime'=>time()]);
            }else{
                $mmodel->editData(['id'=>$mem['id'],'identity'=>2]);
            }

        }catch (\Exception $e) {
            return admin_json(-1,'添加失败'.$e->getMessage());
        }
        writelog('添加'.$name.$id.'的内容',1,$user);

        return admin_json(1, '添加成功');
    }
    /**
     * [postAddmember  添加用户]
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postAddmember()
    {
        $model = new MemberModel();
        $param = input('param.');
        $user = $this->user;
        try{
            if(!isMobile($param['tel'])){
                return admin_json(3,'手机号错误！');
            }
            $member = $model->findData($param['tel'],'tel');
            if(!empty($member)){
                return admin_json(2,'该手机号已存在！');
            }
            $param['pswd'] = md5($param['pswd']);
            $param['addtime'] = time();
            if(empty($param['img'])){
                $param['img'] = config('admin.nouser_img');
            }
            unset($param['token']);
            $model->addData($param);
        } catch (\Exception $e) {
            return admin_json(-1,$e->getMessage());
        }
        writelog('添加用户',1,$user);
        return admin_json(1,'添加成功');
    }
    /**
     * [getOne  获取单挑记录]
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getOne()
    {
        $model = new MemberModel();
        $zmodel = new MzongpaiModel();
        $qmodel = new MemberQuanModel();
        $imodel = new IlikeModel();
        $fmodel = new FriendsModel();
        $data = input('param.');
        $user = $this->user;
        try{
            $member = $model->findData($data['id']);
            if(!empty($member['zid'])){
                $zongpzi = $zmodel->findData($member['zid']);
                $member['zongpai'] = $zongpzi['name'];
            }else{
                $member['zongpai'] = '';
            }
            $map1['member_id'] = $member['id'];
            $quan = $qmodel->getNum($map1);
            $member['quan'] = $quan;
            $member['guanzhu'] = $imodel->getAllCount(['memid'=>$member['id']]);
            $member['friends'] = $fmodel->getAllCount(['memid'=>$member['id']]);
            $member['identity'] = '佛友';
            $http = substr($member['img'],0,4);
            if($http != 'http'){
                $member['img'] = config('domain_name').$member['img'];
            }
        } catch (\Exception $e) {
            return admin_json(-1,$e->getMessage());
        }
        writelog('查看'.$member['username'].'的内容'.$data['id'],1,$user);

        return admin_json(1, '获取成功',$member);
    }
    /**
     * [index  查看推广的人]
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getMembers()
    {
        $model = new MemberModel();
        $data = input('param.');
        $map = [];
        $this->getPageAndSize($data);
        $map['agent_id'] = $data['id'];
        $user = $this->user;
        try {
            $count = $model->getMemberCountByCondition($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getMemberByConditon($map, $this->from, $this->size);
            $zmodel = new MzongpaiModel();
            $qmodel = new MemberQuanModel();
            $imodel = new IlikeModel();
            $fmodel = new FriendsModel();
            foreach($lists as $k=>$v){
                if(!empty($v['zid'])){
                    $zongpzi = $zmodel->findData($v['zid']);
                    $lists[$k]['zongpai'] = $zongpzi['name'];
                }else{
                    $lists[$k]['zongpai'] = '';
                }
                $lists[$k]['addtime'] = date('Y:m:d H:i:s',$lists[$k]['addtime']);
                $map1['member_id'] = $v['id'];
                $quan = $qmodel->getNum($map1);
                $lists[$k]['quan'] = $quan;
                $lists[$k]['guanzhu'] = $imodel->getAllCount(['memid'=>$v['id']]);
                $lists[$k]['friends'] = $fmodel->getAllCount(['memid'=>$v['id']]);
                $lists[$k]['identity'] = '佛友';
            }
        } catch (\Exception $e) {
            return admin_json(-1,$e->getMessage());
        }

        writelog('查看C端用户列表',1,$user);

        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getGather  查看法会、义工报名记录]
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getGather()
    {
        $gmodel = new GatherregModel();
        $data = input('param.');
        $this->getPageAndSize($data);
        $map = [];
        $map['gr.memid'] = $data['id'];
        if(!empty($data['type'])){
            $map['g.type'] = $data['type'];
            if($data['type'] == 1){
                $name = '社会活动';
            }elseif($data['type'] == 2){
                $name = '义工招募';
            }
        }else{
            $name = '活动报名记录';
        }
        $user = $this->user;
        try{
            $field = 'gr.id,gr.addtime,gr.gather_id,g.title,g.starttime,g.maxnum,m.tel,m.username';
            $count = $gmodel->getCounts($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $gmodel->getGatherregByCondition($map,$this->from, $this->size,$field);
            foreach($lists as $k=>$v){
                $num = $gmodel->getCount(['gather_id'=>$v['gather_id']]);
                $lists[$k]['num'] = $num;
                $lists[$k]['addtime'] = date('Y:m:d H:i:s',$v['addtime']);
                $lists[$k]['starttime'] = date('Y:m:d H:i:s',$v['starttime']);
            }
        }catch(\Exception $e){
            return admin_json(-1, '获取失败'.$e->getMessage());
        }

        writelog('查看用户'.$data['id'].'的'.$name,1,$user);
        return admin_json(1,'获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getRecharge  乐善好事]
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getRecharge()
    {
        $model = new RechargeModel();
        $data = input('param.');
        $this->getPageAndSize($data);
        $map = [];
        $user = $this->user;
        $map['r.memid'] = $data['id'];
        $map['r.states'] = 1;
        if(!empty($data['con'])){
            $map['am.tel|am.username'] = ['like', "%" . $data['con'] . "%"];
        }
        if(!empty($data['start']) && !empty($data['end'])){
            $map['r.paytime'] = ['between',[strtotime($data['start']),strtotime($data['end'])]];
        }
        try{
            $count = $model->getCount($map,1);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map,$this->from, $this->size,1);
            foreach($lists as &$v){
                $v['paytime'] = date('Y-m-d H:i:s',$v['paytime']);
            }
        }catch(\Exception $e){
            return admin_json(-1, '获取失败'.$e->getMessage());
        }

        writelog('查看用户'.$data['id'].'的乐善好施',1,$user);
        return admin_json(1,'获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getZongpai  获取宗派]
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getZongpai()
    {
        $data = db('mzongpai')->select();
        return admin_json(1,'获取成功',$data);
    }
    /**
     * [postEditPass  修改密码]
     * @author [C。]
     * @return json
     */
    public function postEditPass()
    {
        $param = input('param.');
        $model = new MemberModel();
        $user = $this->user;
        try{
            $model->editData(['id'=>$param['id'],'pswd'=>md5($param['npass'])]);
        }catch(\Exception $e){
            return admin_json(-1, '修改失败'.$e->getMessage());
        }
        writelog('修改密码--'.$param['id'],1,$user);
        return admin_json(1, '修改成功');
    }
    /**
     * [postEditPass  修改密码]
     * @author [C。]
     * @return json
     */
    public function getMember()
    {
        $param = input('param.');
        $model = new MemberModel();
        $mem = $model->findData($param['id'],'id','username,img,tel,sex');
        $http = substr($mem['img'],0,4);
        if($http != 'http'){
            if(empty($mem['img'])){
                $mem['img'] = 'https://fushan.oss-cn-hangzhou.aliyuncs.com/nouser.png';
            }else{
                $mem['img'] = config('domain_name').$mem['img'];
            }
        }
        return admin_json(1, '获取成功',$mem);
    }
    /**
     * [getAgent  获取推广人]
     * @author [C。]
     * @return json
     */
    public function getAgent()
    {
        $param = input('param.');
        $model = new MemberModel();
        $zmodel = new MzongpaiModel;
        $qmodel = new MemberQuanModel();
        $fmodel = new FriendsModel;
        $imodel = new IlikeModel();
        $jmodel = new MjifenModel();
        $bmodel = new BuddhistServiceModel();
        $gmodel = new GatherregModel();
        $cmodel = new CompanyGongdengOrderModel;
        $remodel = new RechargeModel;
        $ncmodel = new NewsCommentModel;
        $this->getPageAndSize($param);
        $map = [];
        try{
            $map['agent_id'] = $param['id'];
            $field = 'id,tel,username,img,jifen,addtime,city,bathday,zid,identity,qrcode,token';
            $count = $model->getMemberCountByCondition($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getMemberByConditon($map,$this->from, $this->size,$field);
            foreach($lists as &$v){
                if(!empty($v['zid'])){
                    $zongpai = $zmodel->findData($v['zid'],'id');
                    $v['zongpai'] = $zongpai['name'];
                }else{
                    $v['zongpai'] = '无';
                }
                $v['friend_num'] = $fmodel->getAllCount(['memid|friend_id'=>$v['id'],'states'=>1]);
                $quan = $qmodel->getNum(['member_id'=>$v['id']]);
                $v['quan'] = $quan;

                $v['guanzhu'] = $imodel->getCount(['i.memid'=>$v['id'],'i.status'=>1]);
                if(!empty($v['qrcode'])){
                    $v['qrcode'] = 'http://'.$_SERVER['HTTP_HOST'].'/'.$v['qrcode'];
                }else{
                    $qrcode = Qrcode($v['token'],'https://wx.fushan8.com/#/invitePage?id='.$v['id']);
                    $model->editData(['id'=>$v['id'],'qrcode'=>$qrcode]);
                    $v['qrcode'] = 'http://'.$_SERVER['HTTP_HOST'].'/'.$qrcode;
                }
                $v['invite_url'] = 'https://wx.fushan8.com/#/invitePage?id='.$v['id'];
                $v['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                $v['invite_num'] = $jmodel->getAllCount(['memid'=>$v['id']]);
                $v['buddhist_num'] = $bmodel->getAllCount(['memid'=>$v['id']]);
                $v['baoming_num'] = $gmodel->getAllCount(['memid'=>$v['id']]);
                $cnum = $cmodel->getSum(['mid'=>$v['id'],'status'=>1],'total_price');
                $renum = $remodel->getSum(['memid'=>$v['id'],'states'=>1],'amt');
                $v['shankuan'] = $cnum+$renum;
                $v['pinlun'] = $ncmodel->getAllCount(['mid'=>$v['id']]);
                $http = substr($v['img'],0,4);
                if($http != 'http'){
                    $v['img'] = config('domain_name').$v['img'];
                }
            }
        }catch(\Exception $e){
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1,'获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [postEditPass  修改密码]
     * @author [C。]
     * @return json
     */
    public function getMemberbytel()
    {
        $param = input('param.');
        $model = new MemberModel();
        try{
            $data = $model->findOne(['tel'=>$param['tel']],'username,img');
            if(empty($data)){
                return admin_json(-1, '用户不存在');
            }
            $http = substr($data['img'],0,4);
            if($http != 'http'){
                $data['img'] = config('domain_name').$data['img'];
            }
        }catch(\Exception $e){
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
    //修改agent_id
    public function getAgent1()
    {
        $res = Db::table('app_agent')->select();
        foreach($res as $k=>$v){
            $ress = Db::table('app_member')->where('tel',$v['agent_mobile'])->find();
            $res[$k]['id'] = $ress['id'];
            $xiaji = Db::table('app_member')->field('id,agent_id')->where('agent_id',$ress['id'])->select();
            $res[$k]['ids'] = $xiaji;
//            if(!empty($xiaji)){
//                foreach($xiaji as $k1=>$v1){
//                    Db::table('app_member')->where('id', $v1['id'])->update(['agent_id' => $ress['id']]);
//                }
//
//            }

        }
        echo '<pre>';
        print_r($res);
    }
}